package lily.phs.diabet;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import lily.phs.child.ChildScrollActivity;
import lily.phs.child.ImageList;
import lily.phs.database.Constants;
import lily.phs.entertain.YuleActivity;
import lily.phs.epidemic.EpidemicList;
import lily.phs.healthedu.HealthMain;
import lily.phs.object.ModuleSelect;
import lily.phs.urh.PeopleScrollActivity;
import lily.phs.urh.R;
import lily.phs.vaccination.VaccinationScrollActivity;


import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

/**
 * 2型糖尿病患者随访信息管理：
 * page1
 * page2
 * @author heckassitant
 *
 */
public class DiabeterDetails extends Activity  implements OnClickListener,Constants{
	
	private static final String[] nameOfModuleList = {
    	"城乡居民健康档案管理","健康教育","疫情上报","卫生监督","寓教于乐"};
    private static final int[] imageOfModuleList = {
    	R.drawable.peoplehealth,R.drawable.health_education,R.drawable.yiqingshangbao,
    	R.drawable.weishengjiandu,R.drawable.entertain};
	private DiabetServiceView_1 diabetView_1;
	private DiabetServiceView_2 diabetView_2;
	
	private Button btnBack,btnAdd,btnSave,btnUpdate;
	private DiabetScrollLayout mRoot;
	private ListView listAccount;
	private GridView gv2;
	private int profileID=0,father,call;
	private int pageIndex;
	private ProgressDialog mpDialog;
	
//	public Handler mHandler = new Handler(){
//    	@Override
//    	public void handleMessage(Message msg) {
//    		super.handleMessage(msg);
//			int state = msg.what;
//			if(state==1){
//				pageIndex = mRoot.getCurScreen();
//				switch(pageIndex){
//				case 0:{
//					btnAdd.setVisibility(View.GONE);
//					break;
//				}
//				case 1:
//				case 2:{
//					btnAdd.setVisibility(View.VISIBLE);
//					break;
//				}
//				}
//			}
//    	}
//    };

	@Override
	protected void onCreate(Bundle savedInstanceState){
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.diabeter_details);
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.i_title_2);
        
        btnBack = (Button)this.findViewById(R.id.title_left_btn);
        btnBack.setText("后退");
        btnBack.setOnClickListener(this);
        btnAdd = (Button)this.findViewById(R.id.title_right_btn0);
        btnAdd.setVisibility(View.INVISIBLE);
        btnSave = (Button)this.findViewById(R.id.title_right_btn);
        btnSave.setText("保存");
        btnSave.setOnClickListener(this);
        btnUpdate = (Button)this.findViewById(R.id.title_right_btn2);
        btnUpdate.setText("提交");
        btnUpdate.setOnClickListener(this);
        
        profileID = getIntent().getExtras().getInt("Profile_id");
        System.out.println("getIntent().getExtras().getInt(Profile_id);"+getIntent().getExtras().getInt("Profile_id"));
        father = getIntent().getExtras().getInt("father");
        call = getIntent().getExtras().getInt("call");
        
        mRoot = (DiabetScrollLayout) findViewById(R.id.root);
        initViews();

        mRoot.setToScreen(0);
        pageIndex=0;
        
        gv2 = (GridView) findViewById(R.id.gride2);
        ImageList adapter2 = new ImageList(this,imageOfModuleList,nameOfModuleList);
		gv2.setAdapter(adapter2);
		gv2.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				switch (arg2) {
				case 0:{//健康档案
					if(father==MODULE_FATHER){
						Intent editprofile = new Intent();
						Bundle bd = new Bundle();
						bd.putInt("Profile_id", profileID);//待编辑档案的ID
						bd.putInt("father", MODULE_CHILD);
						bd.putInt("call", MODULE_VACCINATION);
						editprofile.putExtras(bd);
						editprofile.setClass(DiabeterDetails.this, PeopleScrollActivity.class);
						startActivity(editprofile);
						overridePendingTransition(R.anim.anim_slide_in_right,R.anim.anim_slide_out_left);
					}
					else if(father==MODULE_CHILD){
						if(call==MODULE_PEOPLE_HEALTH){
							finish();
							overridePendingTransition(R.anim.anim_slide_in_left,R.anim.anim_slide_out_right);
						}
						else{
							Intent editprofile = new Intent();
							Bundle bd = new Bundle();
							bd.putInt("Profile_id", profileID);//待编辑档案的ID
							bd.putInt("father", MODULE_CHILD);
							bd.putInt("call", call);
							editprofile.putExtras(bd);
							editprofile.setClass(DiabeterDetails.this, PeopleScrollActivity.class);
							startActivity(editprofile);
							overridePendingTransition(R.anim.anim_slide_in_right,R.anim.anim_slide_out_left);
							finish();
						}
					}
					break;
				}
				case 1:{//健康教育
					if(father==MODULE_FATHER){
						Intent editprofile = new Intent();
						Bundle bd = new Bundle();
						bd.putInt("Profile_id", profileID);//待编辑档案的ID
						bd.putInt("father", MODULE_CHILD);
						bd.putInt("call", MODULE_PEOPLE_HEALTH);
						editprofile.putExtras(bd);
						editprofile.setClass(DiabeterDetails.this, HealthMain.class);
						startActivity(editprofile);
						overridePendingTransition(R.anim.anim_slide_in_right,R.anim.anim_slide_out_left);
					}
					else if(father==MODULE_CHILD){
						if(call==MODULE_CHILD_MANAGEMENT){
							finish();
							overridePendingTransition(R.anim.anim_slide_in_left,R.anim.anim_slide_out_right);
						}
						else{
							Intent editprofile = new Intent();
							Bundle bd = new Bundle();
							bd.putInt("Profile_id", profileID);//待编辑档案的ID
							bd.putInt("father", MODULE_CHILD);
							bd.putInt("call", call);
							editprofile.putExtras(bd);
							editprofile.setClass(DiabeterDetails.this, HealthMain.class);
							startActivity(editprofile);
							overridePendingTransition(R.anim.anim_slide_in_right,R.anim.anim_slide_out_left);
							finish();
						}
					}
					break;
				}
				case 2:{//疫情上报
					if(father==MODULE_FATHER){
						Intent editprofile = new Intent();
						Bundle bd = new Bundle();
						bd.putInt("Profile_id", profileID);//待编辑档案的ID
						bd.putInt("father", MODULE_CHILD);
						bd.putInt("call", MODULE_PEOPLE_HEALTH);
						editprofile.putExtras(bd);
						editprofile.setClass(DiabeterDetails.this, EpidemicList.class);
						startActivity(editprofile);
						overridePendingTransition(R.anim.anim_slide_in_right,R.anim.anim_slide_out_left);
					}
					else if(father==MODULE_CHILD){
						if(call==MODULE_CHILD_MANAGEMENT){
							finish();
							overridePendingTransition(R.anim.anim_slide_in_left,R.anim.anim_slide_out_right);
						}
						else{
							Intent editprofile = new Intent();
							Bundle bd = new Bundle();
							bd.putInt("Profile_id", profileID);//待编辑档案的ID
							bd.putInt("father", MODULE_CHILD);
							bd.putInt("call", call);
							editprofile.putExtras(bd);
							editprofile.setClass(DiabeterDetails.this, EpidemicList.class);
							startActivity(editprofile);
							overridePendingTransition(R.anim.anim_slide_in_right,R.anim.anim_slide_out_left);
							finish();
						}
					}
					break;
				}
				case 3:{//卫生监督
					
					break;
				}
				case 4:{//寓教于乐
					if(father==MODULE_FATHER){
						Intent editprofile = new Intent();
						Bundle bd = new Bundle();
						bd.putInt("Profile_id", profileID);//待编辑档案的ID
						bd.putInt("father", MODULE_CHILD);
						bd.putInt("call", MODULE_PEOPLE_HEALTH);
						editprofile.putExtras(bd);
						editprofile.setClass(DiabeterDetails.this, YuleActivity.class);
						startActivity(editprofile);
						overridePendingTransition(R.anim.anim_slide_in_right,R.anim.anim_slide_out_left);
					}
					else if(father==MODULE_CHILD){
						if(call==MODULE_CHILD_MANAGEMENT){
							finish();
							overridePendingTransition(R.anim.anim_slide_in_left,R.anim.anim_slide_out_right);
						}
						else{
							Intent editprofile = new Intent();
							Bundle bd = new Bundle();
							bd.putInt("Profile_id", profileID);//待编辑档案的ID
							bd.putInt("father", MODULE_CHILD);
							bd.putInt("call", call);
							editprofile.putExtras(bd);
							editprofile.setClass(DiabeterDetails.this, YuleActivity.class);
							startActivity(editprofile);
							overridePendingTransition(R.anim.anim_slide_in_right,R.anim.anim_slide_out_left);
							finish();
						}
					}
					break;
				}
					
				default:
					break;
				}
			}

		});
		
        mpDialog = new ProgressDialog(DiabeterDetails.this);  
		mpDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);//设置风格为圆形进度条  
		mpDialog.setMessage("正在加载...");  
		mpDialog.setIndeterminate(false);//设置进度条是否为不明确  
		mpDialog.setCancelable(true);//设置进度条是否可以按退回键取消  
		mpDialog.show();
		new myThread(handler).start();
        
	}

	private void initViews() {
		// TODO Auto-generated method stub
		mRoot.removeAllViews();
		diabetView_1 = new DiabetServiceView_1(this,profileID);
		diabetView_2 = new DiabetServiceView_2(this,profileID);
		mRoot.addView(diabetView_1);
		mRoot.addView(diabetView_2);
	}

//	private void initList() {
//		// TODO Auto-generated method stub
//		ArrayList<Map<String, Object>> coll= new ArrayList<Map<String, Object>>();
//        Map<String, Object> item= new HashMap<String, Object>();;
//        item.put("name", "精神病患者个人信息补充表");
//        coll.add(item);
//        item= new HashMap<String, Object>();;
//        item.put("name", "精神病患者随访服务记录表");
//        coll.add(item);
//		listAccount.setAdapter(new SimpleAdapter(this, coll,R.layout.member_listview, new String[] { "name" },new int[] {R.id.txtFather}));
//		// 去掉系统自带的按钮
//		// 去掉系统自带的分隔线
//		listAccount.setDivider(null);
//	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if(v==btnBack){
			new AlertDialog.Builder(DiabeterDetails.this)
			.setIcon(R.drawable.alert_dialog_icon)
			.setTitle("您确定已经保存了要退出吗？")
			.setPositiveButton("确定", new DialogInterface.OnClickListener(){
				public void onClick(DialogInterface dialog, int whichButton) {
					DiabeterDetails.this.finish();
					overridePendingTransition(R.anim.anim_slide_in_left,R.anim.anim_slide_out_right);
				}
			})
			.setNegativeButton("取消", new DialogInterface.OnClickListener() {
		                    public void onClick(DialogInterface dialog, int whichButton) {

		                        /* User clicked Cancel so do some stuff */
		                   }
		                })
		                .create().show();
		}
		else if(v==btnSave){
			pageIndex = mRoot.getCurScreen();
			System.out.println("pageIndex="+pageIndex);
			int i=0;
			switch(pageIndex){
			case 0:
				diabetView_1.saveViews();
				 i=0;
				break;
			case 1:
				diabetView_2.saveViews();
				 i=1;
				break;
			
			}
			Toast.makeText(this, "随访信息保存成功!", Toast.LENGTH_SHORT).show();
		}
	}
	
	public class myThread extends Thread{  
		private Handler handler;

		public myThread(Handler handler){
			this.handler=handler;

		}
		
		public void run(){  
	        try  
	        {   
	        	Looper.getMainLooper();
	        	sleep(1000); 
	        	
	        	diabetView_1.fetchData();
	        	diabetView_2.findViewsById();
	        	diabetView_2.fetchData();
	        	
	        	Message msg = handler.obtainMessage(); 
	        	msg.what=1;
	        	handler.sendMessage(msg); 
			}  
		    catch (Exception e)  
		    {  
		        e.printStackTrace();  
		    }  
		}  
	}
	
	private final Handler handler = new Handler(Looper.getMainLooper()) {
    	public void handleMessage(Message msg) { 
    		if(msg.what==1){
    			diabetView_1.initViews();
    			diabetView_2.setMyView();
    			diabetView_2.initViews();

    			mpDialog.cancel();
    		}
    	} 
    }; 
}
